<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1, user-scalable=no">
		<title></title>
		<meta http-equiv="expires" content="0">
		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<link rel="stylesheet" href="css/reset.css" />
		<link rel="stylesheet" href="css/mui.min.css" />
		<link rel="stylesheet" href="css/cmms.css" />
		<link rel="stylesheet" href="css/iconfont.css" />
		<link rel="stylesheet" href="css/login.css?time=111" />
		<link rel="stylesheet" href="css/loginUp.css?time=333" />
		<script src="js/vconsole.min.js"></script>
	</head>

	<body>
		<div class="mui-content logBg" id='login' v-cloak>
			<div>
				<div class="login">
					<div class="center">
						<img class="yinuo" :src="imgSrc" alt="" />
					</div>
				</div>
				<div class="loginContainer center">
					<div class="mui-input-row">
						<input type="tel" class="mui-input-clear" placeholder="请输入手机号码" v-model="phone" maxlength="11">
					</div>
					<div class="mui-input-row" v-show="loginState">
						<input type="tel" placeholder="请输入验证码" v-model="vCode">
						<button class="abs getCode" @tap='getCode' :disabled="vCodeBtn">{{codeText}}</button>
					</div>
					<div class="mui-input-row" v-show="!loginState">
						<input type="password" placeholder="请输入密码" v-model="vCode">
					</div>
					<div class="btn">
						<button type="button" @tap='login'>登 录</button>
					</div>
				</div>
				<div class="switchLogin" @tap="switchLogin">
					<span class="basic-color" v-show="loginState">密码登录<i class="iconfont icon-youjiantou"></i></span>
					<span class="basic-color" v-show="!loginState">验证码登录<i class="iconfont icon-youjiantou"></i></span>
				</div>
			</div>
		</div>
		<script src="js/mui.min.js"></script>
		<script src="js/vue.js"></script>
		<script src="js/cmms.js?time=333"></script>
		<script>
			new Vue({
				el: '#login',
				data: {
					phone: '',
					vCode: '', //验证码
					vCodeBtn: false, //获取验证码按钮
					codeText: '立即获取',
					timer: '',
					vCodeBtn: false,
					code: '',
					state: '',
					loginState: true, //登录状态，验证码登录true
					loginName: 'sale',
					isWeChat: '1',
					imgSrc:''
				},
				beforeMount() {
					var vConsole = new VConsole();
					AT.muiAjax('/passport/getLoginUser', 'post', {}, function(realData) {
						location.href = 'pages/sale/index.html';
					});
					this.getUrl();
					if(this.isWeChat!='1') {
						this.weChatLogin();
					}
				},
				mounted() {
					this.imgSrc= "img/"+this.state+"-sale.png";
				},
				beforeDestroy: function() {
					clearInterval(this.timer)
				},
				methods: {
					/*获取微信端URI后的code和state*/
					getUrl() {
						let _this = this;
						var url = location.search; //获取url中"?"符后的字串    
						var theRequest = new Object();
						console.log(url);
						if(url.indexOf("?") != -1) {
							var str = url.substr(1);
							//					      console.log(str);
							strs = str.split("&");
							for(var i = 0; i < strs.length; i++) {
								theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
							}
						}
						console.log(theRequest);
						_this.code = theRequest.code;
						_this.state = theRequest.state;
						_this.isWeChat = theRequest.isWechat;
						console.log(_this.code);
						console.log(_this.state);
						console.log( theRequest.isWechat);
					},
					weChatLogin() {
						let _this = this;
						//微信登录
						mui.ajax(AT.baseUrl + '/passport/wechatLogin', {
							data: {
								wechatAppId: _this.state,
								appType: "3",
								oauthCode: _this.code,
								userType: "2"
							},
							dataType: 'json', //服务器返回json格式数据
							type: 'post', //HTTP请求类型
							timeout: 10000, //超时时间设置为10秒；
							crossDomain: true,
							headers: {
								'Content-Type': 'application/json',
								'token': localStorage.getItem(localStorage.getItem("state") + "_sale_token")
							},
							success: (res) => {
//								console.log(location.href)
//								console.log(res)
//								alert(res.msg)
								if(res.code === 0) {
									let realData = res.data;
									console.log(realData);
									localStorage.setItem('state', _this.state);
									localStorage.setItem('loginName', _this.loginName);
									localStorage.setItem(_this.state + '_sale_token', realData.token);
									localStorage.setItem('roamFlag', false);
									location.href = 'pages/sale/index.html';
								}else {
									console.log(AT.weixinUrl(_this.state,'login'))
									window.location.href = AT.weixinUrl(_this.state,'login');
								}
							},
							error: (xhr, type, errorThrown) => {
								console.log('err' + type);
							}
						});
					},
					//验证码
					getCode: function() {
						let _this = this;
						if(_this.phone == '') {
							mui.alert('请输入手机号');
							return false;
						}
						AT.muiAjax('/common/common/sendMessage', 'post', {
							mobile: _this.phone
						}, function(realData) {
							console.log(realData);
						});
						let time = 60;
						_this.vCodeBtn = true;
						_this.timer = setInterval(function() {
							_this.codeText = '重新获取(' + time + ')S';
							time--;
							if(time < 0) {
								clearInterval(_this.timer);
								_this.vCodeBtn = false;
								_this.codeText = '获取验证码';
							}
						}, 1000)
					},
					login: function() {
						let _this = this;
						if(_this.phone == '') {
							mui.alert('请输入手机号');
							return false;
						}
						if(this.loginState == true) {
							if(_this.vCode == '') {
								mui.alert('请输入验证码');
								return false;
							}
							//验证码登录
							AT.muiAjax('/passport/mobileLogin', 'post', {
								wechatAppId: _this.state,
								appType: "3",
								mobile: _this.phone,
								oauthCode: _this.code,
								smsCode: _this.vCode,
								userType: "2"
							}, function(realData) {
								localStorage.setItem('state', _this.state);
								localStorage.setItem('loginName', _this.loginName);
								localStorage.setItem(_this.state + '_sale_token', realData.token);
								localStorage.setItem('roamFlag', false);
								location.href = 'pages/sale/index.html';
							});
						} else if(this.loginState == false) {
							if(_this.vCode == '') {
								mui.alert('请输入密码');
								return false;
							}
							//密码登录
							AT.muiAjax('/passport/login', 'post', {
								wechatAppId: _this.state,
								oauthCode: _this.code,
								appType: "3",
								mobile: _this.phone,
								password: _this.vCode,
								userType: "2"
							}, function(realData) {
								localStorage.setItem('state', _this.state);
								localStorage.setItem('loginName', _this.loginName);
								localStorage.setItem(_this.state + '_sale_token', realData.token);
								localStorage.setItem('roamFlag', false);
								window.location.href = 'pages/sale/index.html';
							});
						}
					},
					switchLogin() {
						this.loginState = !this.loginState;
						this.vCode = '';
					}
				}
			})
		</script>
	</body>

</html>